package com.master.equipment.netty.client;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * @author 洲影
 * @desc TODO
 * @date 2022/6/16
 */
public class Client {

    static final String HOST = "127.0.0.1";
    static final int PORT = 20001;
    static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));

//    http://o26593e113.oicp.vip
    public static void main(String[] args) throws Exception {
    	String message = "0e0010013c0e013836343438373037383534353839383436303031363631383032333835333839383630313138383032383333373135313338271f41007900000b007f";
    	
    	String header = StrUtil.sub(message.toString(), 0, 4);
		String version = StrUtil.sub(message.toString(), 4, 6);
		String daType = StrUtil.sub(message.toString(), 6, 8);
		String dateLen = StrUtil.sub(message.toString(), 8, 10);
		String deviceType = StrUtil.sub(message.toString(), 10, 12);
		String dataType = StrUtil.sub(message.toString(), 12, 14);
		String deviceNo = StrUtil.sub(message.toString(), 14, 44);
		String imsi = StrUtil.sub(message.toString(), 44, 74);
		String iccid = StrUtil.sub(message.toString(), 74, 114);
		
		String cellVoltage = StrUtil.sub(message.toString(), 114, 116);
		String signal = StrUtil.sub(message.toString(), 116, 118);
		String electricQuantity = StrUtil.sub(message.toString(), 118, 120);
		
		String deviceVersion = StrUtil.sub(message.toString(), 120, 124);
		String backUp = StrUtil.sub(message.toString(), 124, 128);
		String frameNumber = StrUtil.sub(message.toString(), 128, 130);
		String verify = StrUtil.sub(message.toString(), 130, 132);
		String end = StrUtil.sub(message.toString(), 132, 134);
		
		
//    	System.out.println(header);
//    	System.out.println(version);
//    	System.out.println(daType);
//    	System.out.println(dateLen);
//    	System.out.println(deviceType);
//    	System.out.println(dataType);
//    	System.out.println(deviceNo);
//    	System.out.println(imsi);
//    	System.out.println(iccid);
//    	System.out.println(cellVoltage);
//    	System.out.println(signal);
//    	System.out.println(electricQuantity);
//    	System.out.println(deviceVersion);
//    	System.out.println(backUp);
//    	System.out.println(frameNumber);
//    	System.out.println(verify);
//    	System.out.println(end);

    	String replay = "0E00010103010001007F";
    	String b = HexUtil.encodeHexStr(replay);
    	System.out.println(b);
    	
    	
    	String replay1 = "0E0001073A070D020B166028762970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001007F";
    	String b1 = HexUtil.encodeHexStr(replay1);
    	System.out.println(b1);
    	
//    	String a = "80000F3836313231343036333436383939300016746573742E6269676E62696F742E636F6D3A3939303200000000";
//    	String b = HexUtil.decodeHexStr(a);
//    	System.out.println(b);
//    	String b = HexUtil.decodeHexStr(hex);
    	
    	
//    	byte[] byteArray = javax.xml.bind.DatatypeConverter.parseHexBinary(a); // 将16进制字符串转换成字节数组
//
//        String text = new String(byteArray); // 将字节数组转换成字符串
//
//        System.out.println("解析结果：" + text); // 输出解析结果
	
    	
//    	String str = "864487078545898";
//
//    	String hex = HexUtil.encodeHexStr(a, CharsetUtil.CHARSET_UTF_8);
//    	System.out.println(hex);
//
//    	//hex是：
//    	//e68891e698afe4b880e4b8aae5ad97e7aca6e4b8b2
//
//    	String decodedStr = HexUtil.decodeHexStr(hex);
//    	System.out.println(decodedStr);
    	
    	
//    	 sendMessage("");
//        sendMessage("[DW*355827990350305*0010*KA,060722,0,0,92]");
//        sendMessage("[DW*355827990350305*010A*UD_LTE,060722,090708,V,0.0,N,0.0,E,22.0,0,-1,0,100,91,0,0,00000008,1,1,460,01,61728,139569695,100,5,YLKJ,74:05:a5:0d:6a:2c,-47,ChinaNet-JYYk,18:aa:ca:c4:40:7b,-58,H3C_longbing,80:e4:55:b3:53:42,-56,CDYLKJ,40:77:a9:60:be:5b,-60,ChinaNet-rYur,04:6b:25:d9:80:2a,-56,0.0]");
//        sendMessage("[DW*355827990350305*0108*AL_LTE,230622,101526,V,30.537387,N,104.411871,E,30.86203,303.514,502.345,17,100,77,0,0,00010008,1,1,460,01,61728,117819171,100,4,DIRECT-zPUSER-20211119DYmsoL,22:03:8c:cf:65:33,-52,CU_ftCs,88:ac:9e:66:5f:de,-56,LPLB,c0:b1:01:28:be:b0,-58,,2a:4c:b4:0d:94:ca,-83,4.013374]");
//		String ceond = "[DW*355827990350305*0108*AL_LTE,230622,101526,V,30.537387,N,104.411871,E,30.86203,303.514,502.345,17,100,77,0,0,00010008,1,1,460,01,61728,117819171,100,4,DIRECT-zPUSER-20211119DYmsoL,22:03:8c:cf:65:33,-52,CU_ftCs,88:ac:9e:66:5f:de,-56,LPLB,c0:b1:01:28:be:b0,-58,,2a:4c:b4:0d:94:ca,-83,4.013374][DW*355827990350305*0108*AL_LTE,230622,101526,V,30.537387,N,104.411871,E,30.86203,303.514,502.345,17,100,77,0,0,00010008,1,1,460,01,61728,117819171,100,4,DIRECT-zPUSER-20211119DYmsoL,22:03:8c:cf:65:33,-52,CU_ftCs,88:ac:9e:66:5f:de,-56,LPLB,c0:b1:01:28:be:b0,-58,,2a:4c:b4:0d:94:ca,-83,4.013376]";
	}
    
    

    public static void sendMessage(String content) throws InterruptedException {
        // Configure the client.
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline p = ch.pipeline();
                            p.addLast("decoder", new StringDecoder());
                            p.addLast("encoder", new StringEncoder());
                            p.addLast(new ClientHandler());
                        }

                    });

            ChannelFuture future = b.connect(HOST, PORT).sync();
            future.channel().writeAndFlush(content);
            future.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }
}
